/*
 * ParseHideFS.java
 *
 * Created on 24.06.2008, 12:42
 *
 * Copyright (c) 2006-2008, Daniel Apatin (ad), http://apatin.net.ru
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * You can also redistribute and/or modify this program under the
 * terms of the Psi License, specified in the accompanied COPYING
 * file, as published by the Psi Project; either dated January 1st,
 * 2005, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package io.file;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Vector;
import midlet.StaticData;

/**
 *
 * @author ad
 */
public class ParseHideFS
    implements Runnable{
    
    StaticData sd=StaticData.getInstance();
    
    public Hashtable hiddenFS = new Hashtable();
    
    /** Creates a new instance of ParseHideFS */
    public ParseHideFS() {
        Thread thread;
        (thread = new Thread(this)).start();
    }

    public void run() {
        hiddenFS.clear();
        try {
            InputStream inputstream = getClass().getResourceAsStream("/fs.txt");
            String s = "";
            do {
                int i;
                if((i = inputstream.read()) == -1)
                    break;
                
                if(i != 10) {
                    if(i == 13) {
                        //System.out.println("p - "+s);
                        addValue(s.trim());
                        s = "";
                    } else {
                        s = s + textConvert(i);
                    }
                }
            } while(true);
            addValue(s);
            System.out.println("hidden fs parsed");
        } catch(IOException _ex) { }
        sd.hiddenFS=this.hiddenFS;
        sd.tasklist.animation(false);
    }

    private void addValue(String s) {
        if(!s.equals("")) {
            String s1 = getStart(s);
            Object obj;
            if((obj = hiddenFS.get(s1)) == null)
                hiddenFS.put(s1, obj = new Vector());
            if(!(s1 = getEnd(s)).equals(""))
                ((Vector)obj).addElement(s1);
        }
    }
    
    public static String getStart(String src) {
        String str;
        int pos;
        if((pos = (str = src.trim()).lastIndexOf('/')) != -1)
            str = str.substring(0, pos + 1);
        else
            str = "";
        return str;
    }
    
    public static String getEnd(String src) {
        String str;
        return str = (str = src.trim()).substring(str.lastIndexOf('/') + 1);
    }
    
    public static char textConvert(int charItem) { //for 1251
        if(charItem < 0)
            charItem += 256;
        char c1 = (char)charItem;
        if(charItem == 168)
            return '\u0401';
        if(charItem == 184)
            return '\u0451';
        if(charItem >= 192 && charItem <= 255)
            return (char)(charItem + 848);
        else
            return c1;
    }
    
}
